package com.whub507.msgmanager.service.impl;

import com.whub507.msgmanager.config.ZWWWConfig;
import com.whub507.msgmanager.domain.Message;
import com.whub507.msgmanager.mapper.MessageMapper;
import com.whub507.msgmanager.mapper.ZwwwMapper;
import com.whub507.msgmanager.service.SystemInitService;
import com.whub507.msgmanager.task.ScheduledTaskManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

@Service
public class SystemInitServiceImpl implements SystemInitService {
    @Resource
    MessageMapper messageMapper;


    @Autowired
    ZWWWConfig zwwwConfig;

    @Resource
    ScheduledTaskManager scheduledTaskManager;
    @Override
    public Boolean InitConfig() {
        return InitMessageSend() && InitZWWW();
    }

    private Boolean InitMessageSend(){
        List<Message> messageList=messageMapper.selectAllByDone(1);
        // 对于定时发送任务，如果因为服务器问题出现超时，需增加额外处理
        messageList.forEach(m-> {
            try {
                scheduledTaskManager.add(m.getTitle() + "-" + m.getProjectid() + "-" + m.getNode(), m);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        });
        System.out.println("初始化消息队列");
        return Boolean.TRUE;
    }

    private Boolean InitZWWW(){
        zwwwConfig.initZwww();
        return Boolean.TRUE;
    }
}
